package com.flyoil.petromp.utils.Dialog;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cnpc.fyutils.EditText_util;
import com.cnpc.fyutils.SystemUtils;
import com.flyoil.petromp.R;

/**
 * Created by songzhiiyn on 2017/10/26.
 * 填写审批弹框备注的工具类
 */

public class DialogApproveUtil {
    private Activity mContext;
    private PopupWindow popupWindow;
    private TextView tv_unApprove;
    private TextView tv_Approve;
    private TextView tv_number;
    private ImageView img_edt_icon;
    private ImageView img_delete_icon;
    private EditText edt_message;

    public DialogApproveUtil(Activity mContext) {
        this.mContext = mContext;
    }

    public void show_approve_dialog(View view, int number) {
        View v1 = LayoutInflater.from(mContext).inflate(R.layout.dialog_approve_remark, null);// 创建view
        popupWindow = new PopupWindow(v1, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, true);// 实例化popupwindow的方法
        popupWindow.setBackgroundDrawable(new ColorDrawable());
        // 这个方法是为了当点击除了popupwindow的其他区域可以关闭popupwindow的方法
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        if (v1 != null) {
            tv_Approve = v1.findViewById(R.id.tv_dialog_approve_remark_approve);
            tv_unApprove = v1.findViewById(R.id.tv_dialog_approve_remark_unapprove);
            tv_number = v1.findViewById(R.id.tv_dialog_approve_remark_number);
            img_delete_icon = v1.findViewById(R.id.img_dialog_approve_remark_delete);
            img_edt_icon = v1.findViewById(R.id.img_dialog_approve_remark_icon);
            edt_message = v1.findViewById(R.id.edt_dialog_approve_remark_message);
            img_delete_icon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow.dismiss();
                }
            });
            EditText_util.setandshowe_ditnumber(edt_message, tv_number, number);
            setEdtAddTextChangedListener();
            SystemUtils.showKeyBoard(edt_message,mContext);//默认显示软键盘
        }
    }

    /**
     * 设置驳回按钮的点击事件
     *
     * @param onclickListener
     */
    public void setUnApproveOnclickListener(View.OnClickListener onclickListener) {
        if (onclickListener != null && tv_unApprove != null) {
            tv_unApprove.setOnClickListener(onclickListener);
        }
    }

    /**
     * 设置审批按钮的点击事件
     *
     * @param onclickListener
     */
    public void setApproveOnclickListener(View.OnClickListener onclickListener) {
        if (onclickListener != null && tv_Approve != null) {
            tv_Approve.setOnClickListener(onclickListener);
        }
    }

    /**
     * 获取输入框的输入内容
     *
     * @return
     */
    public String getEdtMessage() {
        if (edt_message != null) {
            return edt_message.getText().toString().trim();
        }
        return "";
    }

    /**
     * 关闭弹框
     */
    public void dismissPopupWindow() {
        if (popupWindow != null) {
            popupWindow.dismiss();
        }
    }

    /**
     * 给输入框设置输入监听
     */
    private void setEdtAddTextChangedListener() {
        if (edt_message != null) {
            edt_message.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (edt_message.getText().toString().trim().length() > 0) {
                        if (img_edt_icon != null) {
                            img_edt_icon.setVisibility(View.GONE);
                        }
                    } else {
                        if (img_edt_icon != null) {
                            img_edt_icon.setVisibility(View.VISIBLE);
                        }
                    }
                }
            });
        }
    }

}
